$(function(){
	
	comboboxRoles('searchAllRoles.action');
	function comboboxRoles(url){
		$("#role").combobox({
			valueField: 'id',
			textField: 'text',
			value:"",
			url:url,
			onSelect:function(record){//每次选择时触发
//				console.log(record);//得到label和value
				$.ajax({
		            type: "POST",
		            data: {
		            	role:record.id,
		            },
		            url: "distributePermission.action",
		            dataType: "json",
		            success:function(data){
		            	$('#tt').treegrid('loadData',{
//		            		total:9,
	        				rows:data.rows,
		            	});
		            }
		        });
			},
		});
		
	}
	//function treegrid(){
		/*树形网格*/
		$('#tt').treegrid({
//		    url:'distributePermission.action',
		    title : '权限管理',
//		    fit : true, 
		    height:'460',
		    iconCls:'icon-edit',
		    nowrap: false,
		    showFooter:false,
		    idField:'id',
		    treeField:'region',
		    animate:true,
		    singleSelect:true,
		    checkOnSelect:false,
		    selectOnCheck:false,
		    checkbox:true,
		    columns:[[
		        {field:'region',title:'',width:300},
				{field:'1',title:'添加',width:50,editor:{type:'checkbox',options:{on:'P',off:''}},styler:cellStyler},
				{field:'2',title:'删除',width:50,editor:{type:'checkbox',options:{on:'P',off:''}},styler:cellStyler},
				{field:'3',title:'修改',width:50,editor:{type:'checkbox',options:{on:'P',off:''}},styler:cellStyler},
				{field:'4',title:'查询',width:50,editor:{type:'checkbox',options:{on:'P',off:''}},styler:cellStyler},
				{field:'browse',title:'浏览',width:50,editor:{type:'checkbox',options:{on:'P',off:''}},styler:cellStyler},
				{field:'5',title:'',width:50,editor:{type:'checkbox',options:{on:'P',off:''}},/*formatter:formatCheckbox,*/styler:cellStyler},
		   
		    /*  {field:'region',title:'',width:150},
				{field:'1',title:'添加',width:50,editor:{type:'checkbox',options:{on:'P',off:''}}},
				{field:'2',title:'删除',width:50,editor:{type:'checkbox',options:{on:'P',off:''}}},
				{field:'3',title:'修改',width:50,editor:{type:'checkbox',options:{on:'P',off:''}}},
				{field:'4',title:'查询',width:50,editor:{type:'checkbox',options:{on:'P',off:''}}},
				{field:'browse',title:'浏览',width:50,editor:{type:'checkbox',options:{on:'P',off:''}}},
				{field:'5',title:'',width:50,editor:{type:'checkbox',options:{on:'P',off:''}}},
		   */
		      ]],
		    onLoadSuccess: function () {
		    	beginEditing();//开启编辑状态
		    },
		    toolbar : '#tb'
		});
	//}
	
	
	/*开启编辑状态*/
	function beginEditing(){
		var rows = $("#tt").treegrid('getData');//获取数据
//		console.log("SSS"+rows);
    	for(var i=0; i<rows.length; i++){
			for(var j=0;j<rows[i].children.length;j++){
				//if(i==3&&j==1){
					
				//}else{
					$("#tt").treegrid('beginEdit',rows[i].children[j].id);//进入编辑状态
			//	}
				
			}
		}
	}
	
	/*结束编辑状态*/
	function endEditing(){
//		$("#tt").treegrid('getChanges','updated');
		var rows = $("#tt").treegrid('getData');
		for(var i=0; i<rows.length; i++){
			for(var j=0;j<rows[i].children.length;j++){
				$("#tt").treegrid('endEdit',rows[i].children[j].id);//结束编辑状态
			}
		}
	}
	
	/*修改treegrid单元格的样式*/
	function cellStyler(value,row,index){
//		console.log("WXY"+row);
//		console.log("WXY"+index);
		if(value == "d"){
//			return 'background-color:grey;';
			return 'display:none;';
//			return 'visibility: hidden;';
		}
	}
	
	/*修改权限*/
	$('#btn_modify').click(function(){
		$("#tt").treegrid('getChanges','updated');
		endEditing();//结束编辑状态
		var rows = $("#tt").treegrid('getData');//获取数据
		for(var i=0;i<3;i++){
			for(var j=0;j<rows[i].children.length;j++){
				rows[i].children[j]["5"]="d";
			}
		}
		console.log(rows);
		for(var i=3;i<7;i++){
			for(var j=0;j<rows[i].children.length;j++){
				rows[i].children[j]["1"]="d";
				rows[i].children[j]["2"]="d";
				rows[i].children[j]["3"]="d";
				rows[i].children[j]["4"]="d";
				rows[i].children[j]["browse"]="d";
			}
		}
		var rowObj = JSON.stringify(rows);//把json对象转为字符串
		console.log(rows);
		console.log(rows[0].id);
		//console.log(rows.rows);
		$('#tt').treegrid('loadData',{
//    		total:9,
			rows:rows,
    	});
//		console.log(rowObj);
		$.ajax({
            type: "POST",
            url: "editPermissions.action",
            data: {
            	rowObj:rowObj,
            	role:$("#role").combobox('getValue'),
            },
            dataType: "json",
            success : function(result) {
            	beginEditing();//开启编辑状态
//				var result = eval('(' + result + ')');
//            	cellStyler("d",5,2);
            	/*$('#tt').treegrid('update',{
            		id: 2,
            		row: {
            			
            			field:'5',title:'',width:50,editor:{type:'checkbox',options:{on:'P',off:''},styler:cellStyler
            		}
            	});*/
				if (result.success) {
					$.messager.show({ // 显示正确信息
						title : '提示',
						msg : result.message
					});
				} else {
					$.messager.show({ // 显示错误信息
						title : '错误',
						msg : result.message
					});
				}
			}
            /*success: function(data){
            	beginEditing();//开启编辑状态
            	if (data.success){
					$.messager.show({
						timeout:3000,
						title: '提示',
						msg: data.message
					});
					
				} else {				
					$.messager.show({
						timeout:1000,
						title: '错误',
						msg: data.message
					});
				}
            }*/
        });
	});
	
	
});